from colorama import Fore, init
from hdwallet import HDWallet
from hdwallet.symbols import BTC, ETH
import random
import requests
import os
import time
import platform
from pyfiglet import Figlet
import genWIF

init(autoreset=True)

def title():
    f = Figlet(font='standard')
    print(Fore.LIGHTCYAN_EX + f.renderText("zsuuucom") + Fore.RESET)
    print('v1.1')

def get_clear():
    if 'win' in platform.platform() or 'Windows' in platform.platform():
        os.system('cls')
    elif 'linux' in platform.platform() or 'Linux' in platform.platform():
        os.system('clear')
    elif 'darwin' in platform.platform():
        os.system('clear')
    elif 'mac' in platform.platform() or 'Mac' in platform.platform():
        os.system('clear')
    else:
        raise ValueError('Not Supported Platform: "%s"' % platform.platform())

def get_balance(addr):
    time.sleep(13)
    rl = f"https://blockchain.info/q/addressbalance/{addr}"
    print(rl)
    try:
        req = requests.get(rl).json()
        print(req)
        # ret = dict(req)['balance']
        # return 1
        return int(req)
    except KeyError:
        print("Error: Failed to fetch Bitcoin balance.")
        return 0


def main():
    get_clear()
    title()
    print("<www.zsuuu.com>")
    print(Fore.GREEN, "BTC拾荒，启动！", Fore.RESET)
    time.sleep(1)
    z = 1
    ff = 0
    while True:
        try:
            private_key = "".join(random.choice("0123456789abcdef") for _ in range(64))
            hd_btc: HDWallet = HDWallet(BTC)
            hd_btc.from_private_key(private_key)
            WIF = genWIF.genWIF(private_key)
            btc_addr1 = hd_btc.p2pkh_address()
            btc_addr2 = hd_btc.p2wpkh_address()
            btc_addr3 = hd_btc.p2wpkh_in_p2sh_address()
            btc_addr4 = hd_btc.p2wsh_in_p2sh_address()
            btc_addr5 = hd_btc.p2sh_address()

            print(f"{private_key}\n"
                  f"{btc_addr1}\n"
                  f"{btc_addr2}\n"
                  f"{btc_addr3}\n"
                  f"{btc_addr4}\n"
                  f"{btc_addr5}"
                  f"{WIF}")

            value5 = get_balance(btc_addr5)
            value4 = get_balance(btc_addr4)
            value3 = get_balance(btc_addr3)
            value2 = get_balance(btc_addr2)
            value1 = get_balance(btc_addr1)

            print(f"{value5}\n"
                  f"{value4}\n"
                  f"{value3}\n"
                  f"{value2}\n"
                  f"{value1}")

            get_clear()
            title()

            print(Fore.YELLOW, "Email: hi@zsuuu.com", Fore.RESET)
            print(f"Scan: {z} Found: {ff}")

            print(f"{Fore.WHITE}BTC Address (P2PKH)  | BAL: {Fore.MAGENTA}{value1} | {Fore.YELLOW}{btc_addr1}")
            print(f"{Fore.WHITE}BTC Address (BECH32) | BAL: {Fore.MAGENTA}{value2} | {Fore.YELLOW}{btc_addr2}")
            print(f"{Fore.WHITE}BTC Address (P2WPKH) | BAL: {Fore.MAGENTA}{value3} | {Fore.YELLOW}{btc_addr3}")
            print(f"{Fore.WHITE}BTC Address (P2WSH)  | BAL: {Fore.MAGENTA}{value4} | {Fore.YELLOW}{btc_addr4}")
            print(f"{Fore.WHITE}BTC Address (P2SH)   | BAL: {Fore.MAGENTA}{value5} | {Fore.YELLOW}{btc_addr5}")
            print(f"{Fore.WHITE}Private Key (HEX)    | {Fore.MAGENTA}{private_key}")
            print(f"{Fore.WHITE}Private Key (WIF)    | {Fore.MAGENTA}{WIF}")
            print("=" * 50)

            z += 1

            # 获取当前时间戳（从1970年1月1日00:00:00 UTC到现在的秒数）
            current_timestamp = time.time()
            # print("Current timestamp:", current_timestamp)

            # 将时间戳转换为本地时间（结构化的时间元组）
            current_local_time = time.localtime(current_timestamp)
            # print("Current local time:", current_local_time)

            # 格式化本地时间（可选）
            formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_local_time)
            # print("Formatted local time:", formatted_time)
            open('btcwinlog.txt', 'a').write(f'{formatted_time}\n')

            if value1 > 0:
                ff += 1
                open('btcWin.txt', 'a').write(f'{btc_addr1}\n{private_key}\n{WIF}\n')
            elif value2 > 0:
                ff += 1
                open('btcWin.txt', 'a').write(f'{btc_addr2}\n{private_key}\n{WIF}\n')
            elif value3 > 0:
                ff += 1
                open('btcWin.txt', 'a').write(f'{btc_addr3}\n{private_key}\n{WIF}\n')
            elif value4 > 0:
                ff += 1
                open('btcWin.txt', 'a').write(f'{btc_addr4}\n{private_key}\n{WIF}\n')
            elif value5 > 0:
                ff += 1
                open('btcWin.txt', 'a').write(f'{btc_addr5}\n{private_key}\n{WIF}\n')
            else:
                continue

        except Exception as e:
            print(f"An error occurred: {e}")
            print("重启中")
            time.sleep(2)
            main()

if __name__ == "__main__":
    main()